package com.adapter;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.entity.YoukuVideo;
import com.videoplayer.R;

public class VideoListAdapter extends BaseAdapter {

	private List<YoukuVideo> videoList = new ArrayList<YoukuVideo>();
	private ViewHolder viewHolder;
	public Context c;
	
	public VideoListAdapter(Context c){
		this.c = c;
		
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return videoList.size();
	}

	public void setList(List<YoukuVideo> videoList){
		this.videoList = videoList;
	}
	
	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return videoList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	static class ViewHolder {
		private TextView title;
		private ImageView imgTitle;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		
		if (convertView == null) {
			convertView = View.inflate(c,
					R.layout.list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.title = (TextView) convertView
					.findViewById(R.id.list_name);
			viewHolder.imgTitle = (ImageView) convertView
					.findViewById(R.id.list_img);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(videoList!=null){
			viewHolder.title.setText(videoList.get(arg0).getvTitle());
			new GetCSDNLogoTask().execute(videoList.get(arg0).getvImg());
//			viewHolder.imgTitle.setImageBitmap(getHttpBitmap(videoList.get(arg0).getvImg()));
		}
		return convertView;
	}
	
	class GetCSDNLogoTask extends AsyncTask<String,Integer,Bitmap> {//继承AsyncTask  
		  
        @Override  
        protected Bitmap doInBackground(String... params) {//处理后台执行的任务，在后台线程执行  
            HttpClient hc = new DefaultHttpClient();  
            HttpGet hg = new HttpGet(params[0]);//获取csdn的logo  
            final Bitmap bm;  
            try {  
                HttpResponse hr = hc.execute(hg);  
                bm = BitmapFactory.decodeStream(hr.getEntity().getContent());  
            } catch (Exception e) {  
                  
                return null;  
            }  
            //mImageView.setImageBitmap(result); 不能在后台线程操作ui  
            return bm;  
        }  
          
        protected void onProgressUpdate(Integer... progress) {//在调用publishProgress之后被调用，在ui线程执行  
         }  
  
         protected void onPostExecute(Bitmap result) {//后台任务执行完之后被调用，在ui线程执行  
             if(result != null) {  
                 viewHolder.imgTitle.setImageBitmap(result);  
             }else {  
             }  
         }  
           
         protected void onPreExecute () {//在 doInBackground(Params...)之前被调用，在ui线程执行  
        	 viewHolder.imgTitle.setImageBitmap(null);  
         }  
           
         protected void onCancelled () {//在ui线程执行  
         }  
          
    }  
	
	   /**
     * 获取网落图片资源 
     * @param url
     * @return
     */
    public static Bitmap getHttpBitmap(String url){
    	URL myFileURL;
    	Bitmap bitmap=null;
    	try{
    		myFileURL = new URL(url);
    		//获得连接
    		HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
    		//设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制
    		conn.setConnectTimeout(6000);
    		//连接设置获得数据流
    		conn.setDoInput(true);
    		//不使用缓存
    		conn.setUseCaches(false);
    		//这句可有可无，没有影响
    		//conn.connect();
    		//得到数据流
    		InputStream is = conn.getInputStream();
    		//解析得到图片
    		bitmap = BitmapFactory.decodeStream(is);
    		//关闭数据流
    		is.close();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
		return bitmap;
    	
    }

}
